<?php
class Admin_Form_Resource extends Zend_Form
{
    public function init()
    {
        $this->setName("admin");

        $resourceId = new Zend_Form_Element_Hidden('resourceId');
        $resourceId->addFilter('Int');
        $resourceId->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $resourceName = new Zend_Form_Element_Text('resourceName');
        $resourceName->setLabel('Tên Resource *')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim');
        $resourceName->setAttrib('class','span12');        
        $resourceName->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $moduleName = new Zend_Form_Element_Text('moduleName');
        $moduleName->setLabel('Module Name *')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim');
        $moduleName->setAttrib('class','span6');
        $moduleName->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $controllerName = new Zend_Form_Element_Text('controllerName');
        $controllerName->setLabel('Controller Name *')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim')->addValidator('NotEmpty');
        $controllerName->setAttrib('class','span6');
        $controllerName->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $actionName = new Zend_Form_Element_Text('actionName');
        $actionName->setLabel('Action Name *')->setRequired(true)->addFilter('StripTags')->addFilter('StringTrim')->addValidator('NotEmpty');
        $actionName->setAttrib('class','span6');
        $actionName->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label'); 
        
        $menu = new Zend_Form_Element_Text('menu');
        $menu->setLabel('Menu');                
        $menu->setAttrib('disabled','')->setAttrib('class','span6');
        $menu->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');                                      
        
        $detail = new Zend_Form_Element_Textarea('detail');
        $detail->setLabel('Ghi chú');
        $detail->setAttrib('rows','6')->setAttrib('class','span12');
        $detail->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');                        
        
        $menuName = new Zend_Form_Element_Text('menuName');
        $menuName->setLabel('Thêm mục (Tên menu) *')->setRequired(true)->addValidator('NotEmpty');
        $menuName->setAttrib('class','span6');
        $menuName->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $menuModel = new Zend_Form_Element_Text('menuModel');
        $menuModel->setLabel('Thêm mục (Mã menu) *')->setRequired(true)->addValidator('NotEmpty');
        $menuModel->setAttrib('class','span6');
        $menuModel->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
                   
        $controllerNameShowup = new Zend_Form_Element_Text('controllerNameShowup');
        $controllerNameShowup->setLabel('Controller Name *')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim');
        $controllerNameShowup->setAttrib('class','span6');
        $controllerNameShowup->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $actionNameShowup = new Zend_Form_Element_Text('actionNameShowup');
        $actionNameShowup->setLabel('Action Name *')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim');
        $actionNameShowup->setAttrib('class','span6');
        $actionNameShowup->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
                        
        $ord = new Zend_Form_Element_Text('ord');
        $ord->setLabel('Thứ tự')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim')->addFilter('Int');
        $ord->setValue('1');
        $ord->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $displayTemplate = new Zend_Form_Element_Checkbox('displayTemplate');
        $displayTemplate->setLabel('Thêm vào Menu')->setRequired(true)->addValidator('NotEmpty')->addFilter('StripTags')->addFilter('StringTrim')->addFilter('Int');        
        $displayTemplate->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        //Support Select Menu
        $supportMenu = new Zend_Form_Element_Select('supportMenu');        
        $supportMenu->setLabel('Hỗ trợ chọn nhanh');
        $supportMenu->setAttrib('class','span3');
        $supportMenu->addMultiOption('0','--- Menu ---');
        $supportMenu->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        $modelResource = new Admin_Model_Resource();
        $resourceList = $modelResource->getAllData();
        $listMenu = array();
        foreach($resourceList as $resource){
            if(!in_array($resource['menuModel'], $listMenu)){
                $listMenu[] = $resource['menuModel'];
            }
        }
        foreach($listMenu as $m){
            $supMenuName = $modelResource->getData(array('menuModel'=>$m));            
            $supportMenu->addMultiOption($m,$supMenuName[0]['menuName']);            
        }        
        
        // Control Button
        $submit = new Zend_Form_Element_Submit('submit');        
        $submit->setLabel('Submit & Validate');
        $submit->setAttrib('class','btn btn-blue');
        $submit->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
                
        $reset = new Zend_Form_Element_Reset('reset');        
        $reset->setLabel('Reset');
        $reset->setAttrib('class','btn btn-yellow');
        $reset->removeDecorator('HtmlTag')->removeDecorator('DtDdWrapper')->removeDecorator('Label');
        
        $this->setDecorators(array(
                           array('viewScript',
                           array('viewScript'=>'template/form_resource.phtml'),
                    ))); 
                
        // add elements to form
        $this->addElements(array (
            $resourceId, $resourceName, $moduleName, $controllerName, $actionName, $menu, $detail,
            $supportMenu, $menuName, $menuModel,
            $controllerNameShowup, $actionNameShowup,
            $ord, $displayTemplate,
            $submit, $reset
        ));
    }
}

